package com.hshx.controller;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/websocket/{userid}")
@Component
public class MyWebSocket {
    //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;

    //concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象。
    private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();

    //与某个客户端的连接会话，需要通过它来给客户端发送数据
    private Session session;
    private String userid;

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userid") String userid) throws IOException {
        this.session = session;
        this.userid = userid;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //在线数加1

        System.out.println("有新连接 " + userid + " 加入！当前在线人数为:" + getOnlineCount());
        try {
            this.sendMessage("欢迎" + userid + "进入聊天室！！<br/>");
            this.broadcast();
        } catch (IOException e) {
            System.out.println("IO异常");
        }
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        webSocketSet.remove(this);  //从set中删除
        subOnlineCount();           //在线数减1
        System.out.println("有一连接关闭！当前在线人数为:" + getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("来自客户端的消息:" + message);
        String[] ms = message.split("#");
        if (ms != null && ms.length == 2) {
            //向指定用户发消息
            System.out.println("To:" + ms[1]);
            //根据对方的id得到WebSocket对象
            MyWebSocket mws = getWebSocket(ms[1]);
            if (mws != null) {
                try {
                    mws.sendMessage(ms[0]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            //群发消息
            this.broadcast(message);
        }
    }

    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    //群发消息
    private void broadcast(String message) {
        for (MyWebSocket item : webSocketSet) {
            if(item==this){
                continue;
            }
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //群发在线用户
    private void broadcast() {
        //拼接在线用户
        String users = "users:";
        for (MyWebSocket item : webSocketSet){
            users += item.userid+",";
        }
        users = users.substring(0,users.length()-1);
        System.out.println(users);
        //循环发送
        for (MyWebSocket item : webSocketSet) {
            try {
                item.sendMessage(users);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发生错误时调用
     * @OnError
     */
    public void onError(Session session, Throwable error) {
        System.out.println("发生错误");
        error.printStackTrace();
    }

    /*
     * 控制器传用户id过来获取WebSocket对象推送强制下线消息
     */
    public static MyWebSocket getWebSocket(String userid) {
        if (webSocketSet == null || webSocketSet.size() <= 0) {
            return null;
        }
        for (MyWebSocket item : webSocketSet) {
            if (userid.equals(item.userid)) {
                return item;
            }
        }
        return null;
    }


    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        MyWebSocket.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        MyWebSocket.onlineCount--;
    }

}
